package com.example.blog.controller.admin;

import com.example.blog.pojo.Type;
import com.example.blog.service.TypeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 东北宋小宝
 * date: 2020/8/15
 */
@Slf4j
@Controller
@RequestMapping("/admin")
public class TypeController {
    @Resource
    TypeService typeService;
    @Value("${blogpage.type}")
    private int pageNum;
    /***
     * 展示type页控制器
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/types")
    public String types(Integer page, Model model){
        PageInfo<Type> pageInfo = typeService.listType(page, pageNum);
        model.addAttribute("pages",pageInfo);
        return "admin/types";
    }

    /***
     * 添加type页面控制器
     * @return
     */
    @GetMapping("/typeinput")
    public String typeInput(){
        return "admin/types-input";
    }
    @PostMapping("/types")
    public String types(@Validated Type type, BindingResult result, RedirectAttributes redirectAttributes){
        List<ObjectError> allErrors = result.getAllErrors();
        for (ObjectError error: allErrors
             ) {
            log.error(error.getDefaultMessage());
        }
        if (allErrors.size()>0){
            redirectAttributes.addAttribute("message","添加失败");
            return "redirect:/admin/typeinput";
        }
        try {
            typeService.saveType(type);
        } catch (Exception e) {
            redirectAttributes.addAttribute("message","已有相同标签");
            return "redirect:/admin/typeinput";
        }
        redirectAttributes.addAttribute("message","添加成功！");
        return "redirect:/admin/types";
    }
    @PostMapping("/typeEdit")
    public String edit(Type type, RedirectAttributes redirectAttributes, Model model){
        try {
            typeService.edit(type);
        } catch (Exception e) {
            model.addAttribute("message","已有重复标签！");
            model.addAttribute("type",type);
            return "admin/types-edit";
        }
        redirectAttributes.addAttribute("message","添加成功！");
        return "redirect:/admin/types";
    }
    @GetMapping("/toEdit")
    public String toEdit(Integer id, Model model, RedirectAttributes redirectAttributes){
        Type type = typeService.toEdit(id);
        model.addAttribute("type",type);
        return "admin/types-edit";
    }
    @GetMapping("/delType")
    public String delType(Integer id, RedirectAttributes redirectAttributes){
        typeService.deleteType((long)id);
        redirectAttributes.addAttribute("message","删除成功！");
        return "redirect:/admin/types";
    }
}
